package com.activemq.web.webproducer.controller;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTempTopic;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import java.util.HashMap;
import java.util.Map;

/**
 * @date 2020/10/8 17:03
 */
@RestController
@RequestMapping("msg")
public class SendController {



    @Autowired
    private JmsTemplate jmsTemplate;


    /**
     * 发送队列
     * @return
     */
    @RequestMapping("sendQueue")
    public Map<String ,Object> sendQueue(){
        HashMap<String ,Object> map = new HashMap<String,Object>();
//        设置队列名称
        jmsTemplate.setDefaultDestination(new ActiveMQQueue("queue-hello-web"));
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return  session.createTextMessage("hello-jar-queue");
            }
        });
        map.put("success","发送成功");
        return map;
    }


    /**
     * 发送主题
     * @return
     */
    @RequestMapping("sendTopic")
    public Map<String ,Object> sendTopic(){
        HashMap<String ,Object> map = new HashMap<String,Object>();

//        设置主题名称
        jmsTemplate.setDefaultDestination(new ActiveMQTopic("topic-hello-web"));
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return  session.createTextMessage("hello-jar-topic");
            }
        });
        map.put("success","发送成功");
        return map;
    }

}
